//
//  UIColor+Util.m

#import "UIColor+Util.h"

@implementation UIColor (Util)

- (UIImage *)colorToImage {
    return [self imageWithColorInRect:CGRectMake(0, 0, 1, 1)];
}

- (UIImage *)imageWithColorInRect:(CGRect)rect {
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [self CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIColor *)colorWithHexString:(NSString *)hexString {
    NSString *cstring  = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if (cstring.length < 6) {
        return [UIColor clearColor];
    }
    
    if ([cstring hasPrefix:@"0X"]) {
        cstring = [cstring substringFromIndex:2];
    }
    if ([cstring hasPrefix:@"#"]) {
        cstring = [cstring substringFromIndex:1];
    }
    if (cstring.length != 6) {
        return [UIColor clearColor];
    }
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cstring substringWithRange:range];
    
    range.location = 2;
    //g
    NSString *gString = [cstring substringWithRange:range];
    
    range.location = 4;
    //b
    NSString *bString = [cstring substringWithRange:range];
    
    unsigned int r,g,b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+ (UIColor *)lppz_navigationColor {
    return [self colorWithHexString:@"#E94715"];
}

+ (UIColor *)lppz_blackColor {
    return [self colorWithHexString:@"#353535"];
}

+ (UIColor *)lppz_darkGrayColor {
    return [self colorWithHexString:@"#666666"];
}

+ (UIColor *)lppz_darkGreenColor {
    return [self colorWithHexString:@"#009933"];
}

+ (UIColor *)lppz_backgroundColor {
//    return [self colorWithHexString:@"#F0F0F0"];
    return [self colorWithHexString:@"#f5f5f5"];
}

+ (UIColor *)lppz_priceColor {
    return [self colorWithHexString:@"#E94715"];
}

+ (UIColor *)lppz_lightGrayColor {
    return [self colorWithHexString:@"#999999"];
}

+ (UIColor *)lppz_searchViewColor {
    return [self colorWithHexString:@"#009A57"];
}

+ (UIColor *)lppz_borderColor {
    return [self colorWithHexString:@"#d9d5d4"];
}

+ (UIColor *)lppz_separatorColor {
    return [self colorWithHexString:@"#e7e7e7"];
}

+ (UIColor *)lppz_titleColor {
    return [self colorWithHexString:@"#333333"];
}

+ (UIColor *)lppz_lineColor {
    return [self colorWithHexString:@"#f2f2f2"];
}

+ (UIColor *)lppz_whiteColor {
    return [self colorWithHexString:@"#f5f5f5"];
}

@end
